package com.panda.pandawash.activity;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.panda.pandawash.R;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.util.PermissionUtils;
import com.panda.pandawash.view.StatusBarCompat;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by gaowf on 2017/2/21.
 */

public class PromoteFeedbackActivity extends Activity {


    @ViewInject(R.id.me_to_feedback)
    private TextView feedbacTv;

    @ViewInject(R.id.feedback_record)
    private TextView recordTv;

    @ViewInject(R.id.me_to_promotion)
    private TextView promoteTv;

    @ViewInject(R.id.me_to_promotion_layout)
    private LinearLayout promotell;

    private ImageView mNewPromote;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_promote_feedback);
        x.view().inject(this);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }

        feedbacTv.setOnClickListener(mClickListener);
        recordTv.setOnClickListener(mClickListener);
        promoteTv.setOnClickListener(mClickListener);
        promotell.setOnClickListener(mClickListener);
        mNewPromote = (ImageView) findViewById(R.id.new_promotion_img);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if( !CDApplication.getIntance().getPreferenceUtils().getBoolean("firstLogin",true)){
            mNewPromote.setVisibility(View.GONE);
        }else{
            mNewPromote.setVisibility(View.VISIBLE);
        }

    }


    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.me_to_feedback:
                    Intent intent_feedback = new Intent(PromoteFeedbackActivity.this, FeedbackActivity.class);
                    startActivity(intent_feedback);
                    break;
                case R.id.feedback_record:
                    Intent intent_feedback_record = new Intent(PromoteFeedbackActivity.this, FeedbackRecordActivity.class);
                    startActivity(intent_feedback_record);
                    break;
                case R.id.me_to_promotion_layout:
                case R.id.me_to_promotion:
                    if (PermissionUtils.isLocationEnable(PromoteFeedbackActivity.this)) {
                        requestLocationPermission();
                    } else {
                        requestDialog();//手动打开
                    }
                    break;
            }
        }
    };

    private void requestDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(PromoteFeedbackActivity.this);
        builder.setTitle(getResources().getString(R.string.location_enable_title).toString());
        builder.setMessage(getResources().getString(R.string.location_enable_notification).toString());
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, 0); // 设置完成后返回到原来的界面
            }
        });
        builder.show();
    }


    private void requestLocationPermission() {
        Log.d("gwf_add", "request permission");
        if (ContextCompat.checkSelfPermission(PromoteFeedbackActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(PromoteFeedbackActivity.this, new String[] {Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        } else {
            baiDuMapLocation();
        }
    }

    private void baiDuMapLocation() {
        Intent intent_promotion = new Intent(PromoteFeedbackActivity.this, PromoteActivity.class);
        startActivity(intent_promotion);
    }

    @Event(R.id.promote_feedback_return)
    private void excuteReturn(View v) {
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    baiDuMapLocation();
                } else {
                   // Toast.makeText(this, "申请位置权限被拒绝了", Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
        }
    }

}
